<?php
/**
*数据库类 
*/
	class db
	{
	
		private $host;
		private $user;
		private $pass;
		private $db_name;
		private $charset;
		private $sql;
		private $result;
		private $row;
		private $assoc;
		private $query;
		private $FieldName; //字段名数组
		private $error;
		
		function __construct($host,$user,$pass,$db_name,$charset){
		
			$this->host=$host;
			$this->user=$user;
			$this->pass=$pass;
			$this->db_name=$db_name;
			$this->charset=$charset;
			$this->connect();
			
		}
		
		//-----------连接数据库------------------
		private function connect(){
		
			$link=mysql_connect($this->host,$this->user,$this->pass) or die ($this->error());
			
			mysql_select_db($this->db_name,$link) or die("没该数据库：".$this->table);
			  
			mysql_query("SET NAMES " .$this->charset)	or die ($this->error());  //转码
		}
		
		//--------INSECT INTO------------
		/*
		*     $table  表名
		*
		*	  $value 字段名的关联数组
		*
		*/
		function insert($table,$value){
		
			$t="";
			$field="";
			$FieldName;
			//--------获取字段名--------
			$this->result=$this->query("show fields from $table");
			
		
			while($this->row=$this->fetch_assoc($this->result)){
				
				$FieldName[]=array($this->row['Field']=>'NULL');
				$field=$field.$this->row['Field'].",";
			}
			$field=substr($field, 0, -1);
			

			//---------插入值----------
			$leng=count($FieldName);
				
			foreach($value as $key=>$vo){
				
				for($i=0;$i<$leng;$i++){
				
					if(isset($FieldName[$i][$key])){
					
						$FieldName[$i][$key]=$vo;
						
					}
					
				}
			}
			
			//--------获取插入值----------
			
			foreach($FieldName as $vo){
				
					foreach($vo as $voo){
					
					$t=$t."'$voo'".",";
					}
			}
			$t=substr($t, 0, -1);
			
			$t= str_replace("NULL","",$t);
		
			$this->sql="INSERT INTO $table ($field) VALUES ($t)";
			echo $this->sql;
			 $this->query($this->sql)OR DIE ($this->error());
			
			// return 1;
		}
	
		//-----------SELECT DATA--------------
		/*
		*  $table  table name
		*  $condition  conditionaljudgment for keywords "WHERE"
		*  $order      condition for keywords "ORDER"
		*  $limit      condition  for keywords  "LIMIT"
		*  
		*  return an array;
		*/
		
		function select($table,$condition=NULL,$order=NULL,$limit=NULL){
			
			$data="";
			
			$this->sql="SELECT * FROM $table";
			
			if($condition!=NULL){
			
				$this->sql=$this->sql." WHERE $condition";
			}
			
			if($order!=NULL){
			
				$this->sql=$this->sql." ORDER BY $order";
			}
			
			if($limit!=NULL){
			
				$this->sql=$this->sql." LIMIT $limit";
			}
			echo $this->sql,"<br/>";
			
			$this->result=$this->query($this->sql);
			
			if(!empty($this->result)){
			
				while($this->row=$this->fetch_assoc($this->result)){
				
					$data[]=$this->row;
				}
				
				
			}
			return $data;
		
		}
		//---------UPDATA----------------
		/*
		*   $table  table name
		*	$value   the condition to change
		*   $condition  conditionaljudgment for keywords "WHERE"
		*/
		function update($table,$value,$condition=NULL){
		
			$this->sql="UPDATE $table SET $value";
			
			if($condition!=NULL){
			
				$this->sql=$this->sql." WHERE $condition";
			}
			
			$this->result=$this->query($this->sql) or die ($this->error());
			
		
		}
		//----------DELETE-------------
		
		function delete($table,$condition){
		
			$sql="DELETE FROM $table WHERE $condition";
			
			$this->query($sql)or die($this->error());
		
		}
		
		//-------MYSQL_FETCH_ASSOC--------
		function fetch_assoc($result){
		
			$this->assoc=mysql_fetch_assoc($result);
			
			return $this->assoc;
		}
		
		//-----MYSQL_QUERY----------
		function query($sql) {
		
			$this->query = mysql_query($sql);
			
			return $this->query;
		}
		
		function error(){
		
			return $this->error=mysql_error();
		}
	 
			
	}
	
	
	
//	$db->insert
	
//	$a=$db->select('content',"content_id>'43'","content_id DESC"," 5 ");

	// $data['content_body']="测试2";
	// $data['type']="测试2";
	
	
	// $db->insert('content',$data);
	
	// $db->update('content','content_body="测试"','content_id=96');
	
	// $db->delete('content','content_id=96');
	// foreach($a as $vo){
	
		// echo $vo['content_id'];
	// }
	
	// var_dump($a);

	

?>

